# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Fabricio Silva <hi@fabricio.dev>
pkgname=sonarr
pkgver=4.0.0.748
pkgrel=0
pkgdesc="TV download automation for usenet and torrents."
url="https://github.com/Sonarr/Sonarr"
arch="x86_64 aarch64 armv7"
license="GPL-3.0-only"
options="net !check" # no tests
depends="
	aspnetcore6-runtime
	sqlite-libs
	"
makedepends="
	dotnet6-sdk
	yarn
	"
subpackages="$pkgname-openrc"
install="$pkgname.pre-install"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/Sonarr/Sonarr/archive/refs/tags/v$pkgver.tar.gz
	0001-disable-restart.patch
	sonarr.initd
	sonarr.confd
	package_info
	"
builddir="$srcdir/Sonarr-$pkgver"
pkgusers="sonarr"
pkggroups="sonarr"

# map arch to dotnet
case $CARCH in
	x86_64) _dotnet_arch="x64" ;;
	aarch64) _dotnet_arch="arm64" ;;
	armv7) _dotnet_arch="arm" ;;
	*) _dotnet_arch="$CARCH" ;;
esac

# custom variables
_runtime="linux-musl-$_dotnet_arch"
_framework="net6.0"
_output="_output"
_artifacts="$_output/$_framework/$_runtime/publish"

prepare() {
	default_prepare

	# increase max opened files
	ulimit -n 4096

	# replace version info
	local buildprops=src/Directory.Build.props
	sed -i "/<AssemblyVersion>/s/>.*<\//>$pkgver<\//" "$buildprops"
	sed -i "/<AssemblyConfiguration>/s/>.*<\//>master<\//" "$buildprops"

	export BROWSERSLIST_IGNORE_OLD_DATA=true
	yarn install --frozen-lockfile --network-timeout 120000
}

build() {
	dotnet build src \
		-p:RuntimeIdentifiers="$_runtime" \
		-p:Configuration=Release \
		-p:SelfContained=false \
		-t:PublishAllRids
	yarn build --env production --no-stats

	# fix ffprobe permissions
	chmod +x "$_artifacts"/ffprobe

	# cleanup
	find "$_artifacts" \( \
		-name "ServiceUninstall.*" -o \
		-name "ServiceInstall.*" -o \
		-name "Sonarr.Windows.*" \) -delete

	mv "$_output"/UI "$_artifacts"
}

package() {
	local DESTDIR="$pkgdir"/usr/lib/sonarr

	# use package_info to disable update feature
	install -Dm644 "$srcdir"/package_info "$DESTDIR"/package_info
	echo "PackageVersion=$pkgver-r$pkgrel" >>"$DESTDIR"/package_info

	cp -af "$_artifacts" "$DESTDIR"/bin
	chown -R "$pkgusers:$pkggroups" "$DESTDIR"

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
a2b476099f37b34a6e281757321150674985ffe64cbd4d265e00639338d6e6162352528c22ea0c9c882ea981e98cc053d215e630db8180aa96c49e3bdb0964b9  sonarr-4.0.0.748.tar.gz
753520379a6c5f2899b4ddfd820d5573598a32cb105f74c6fd978a3f2c98895cf709ef2d9afe2bae8c4856da86cb6a1364f5713ea7ef6a36bb21b5a5840c1471  0001-disable-restart.patch
6cda2b7afc5f61dc8460b9e6a1683d8a04b705cf0dfaf8ef79845b797bdf1b4bb582cf217c83f02717d7daaaf337112da8720c400ab1d374f8a6b04a7ee07286  sonarr.initd
d1cf825905ee8847f8094ec27d1b43df51558f0e94b17071cc15d019438c3c7d0c884fcc22876690e8b49d0f9dd3c2893ffcdab0c215282db27e24d0c8c6ec29  sonarr.confd
91274754046fe7b7aade722af2c0a1e00265cb0b43808ad757bb55e780794f2c9466d1036841bb2a6f2e114784585ebef8ad6378cee72197e27a7d2963e022ba  package_info
"
